﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RayDen.Library.Components.Surface;
using RayDen.Library.Data.Xml;
using RayDen.Library.Entity.Frames;
using RayDen.Library.Entity.Interface;
using RayDen.RayEngine;

namespace RayDen.MaterialManager.Controllers
{
    public class RenderingController : IDisposable
    {
        private MaterialViewRayEngineConfiguration config;
        string frameName = @"I:\Dev\Frames\RDF\mat_view.rdF";

        public RenderingController(SurfaceMaterial mat, int w, int h)
        {
            FrameDescription frame;

            config = new MaterialViewRayEngineConfiguration(mat, mat);
            SetupRenderer();
            using (var frameFile = new FileStream(frameName, FileMode.Open, FileAccess.Read))
            {
                using (var reader = new StreamReader(frameFile))
                    frame = (FrameDescription)SerializationService.Deserialize(reader.ReadToEnd(), typeof(FrameDescription));
            }
            frame["ImageWidth"] = w;
            frame["ImageHeight"] = h;
            frame.FrameName = Path.GetFileNameWithoutExtension(frameName);
            config.OpenFrame(frame);
        }

        public IRenderConfig RenderConfig
        {
            get { return config; }
        }

        protected void SetupRenderer()
        {
            
        }

        public void StartRender(PictureBox canvas)
        {
            config.Start();
        }

        public void Interrupt()
        {
            config.Interrupt();
        }

        public void SaveHdr(string filename)
        {
            
        }

        public void Dispose()
        {
            config.Dispose();            
        }
    }
}
